package com.woniuxy.book.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.book.entity.Account;
import com.woniuxy.book.entity.PageInfo;
import com.woniuxy.book.exception.account.AccountException;
import com.woniuxy.book.exception.account.AccountExceptionCode;
import com.woniuxy.book.mapper.AccountMapper;
import com.woniuxy.book.param.LoginParam;
import com.woniuxy.book.param.account.AddAccountParam;
import com.woniuxy.book.param.account.SelectAdminParam;
import com.woniuxy.book.param.account.UpdateAdminParam;
import com.woniuxy.book.service.AccountService;
import com.woniuxy.book.service.dto.AccountDTO;
import com.woniuxy.book.util.StateData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author li
 * @since 2024年2月09日
 */
@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;
    @Override
    public AccountDTO login(LoginParam param) {
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_name",param.getAccountName());
        Account account = accountMapper.selectOne(queryWrapper);
        if (account==null) {
            throw new AccountException(AccountExceptionCode.ACCOUNT_EXCEPTION_ACCOUNT_NOT_EXIST);
        }
        if (!account.getAccountPass().equals(param.getAccountPass())) {
            throw new AccountException(AccountExceptionCode.ACCOUNT_EXCEPTION_PASSWORD_ERROR);
        }
        if (account.getAccountState()!= StateData.ADMIN_STATE_NORMAL) {
            throw new AccountException(AccountExceptionCode.ACCOUNT_EXCEPTION_ACCOUNR_NOT_NORMAL);
        }
        return BeanUtil.toBean(account,AccountDTO.class);
    }

    @Override
    @Transactional
    public void addAccount(AddAccountParam param) {
        QueryWrapper<Account> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("account_name",param.getAccountName());
        if (accountMapper.selectOne(queryWrapper1)!=null) {
            throw new AccountException(AccountExceptionCode.ACCOUNT_EXCEPTION_NAME_REPEAT);
        }
        QueryWrapper<Account> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("account_phone",param.getAccountPhone());
        if (accountMapper.selectOne(queryWrapper2)!=null) {
            throw new AccountException(AccountExceptionCode.ACCOUNT_EXCEPTION_PHONE_REPEAT);
        }

        Account account = BeanUtil.toBean(param, Account.class);
        account.setAccountState(StateData.ADMIN_STATE_NORMAL);
        accountMapper.insert(account);
    }

    @Override
    public List<AccountDTO> selectAdmin(SelectAdminParam param) {
        QueryWrapper<Account> wrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(param.getAccountNickname())) {
            wrapper.like("account_nickname",param.getAccountNickname());
        }
        if (ObjectUtil.isNotEmpty(param.getAccountPhone())) {
            wrapper.like("account_phone",param.getAccountPhone());
        }
        if (ObjectUtil.isNotEmpty(param.getAccountState())) {
            wrapper.eq("account_state",param.getAccountState());
        }
        List<Account> accounts = accountMapper.selectList(wrapper);
        ArrayList<AccountDTO> accountDTOS = new ArrayList<>();
        for (Account account : accounts) {
            AccountDTO accountDTO = BeanUtil.toBean(account, AccountDTO.class);
            accountDTOS.add(accountDTO);
        }
        return accountDTOS;
    }

    @Override
    public PageInfo searchAdmin(Integer pageNum, Integer pageSize, String condition) {
        QueryWrapper<Account> wrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(condition)) {
            wrapper.like("account_nickname",condition).or().like("account_phone",condition);
        }
        Page<Account> page = new Page<Account>(pageNum==null?1:pageNum,pageSize==null?5:pageSize);
        accountMapper.selectPage(page,wrapper);
        PageInfo pageInfo = BeanUtil.toBean(page,PageInfo.class);
        pageInfo.setPages(page.getPages());
        ArrayList<AccountDTO> accountDTOS = new ArrayList<>();
        for (Account account : page.getRecords()) {
            AccountDTO accountDTO = BeanUtil.toBean(account, AccountDTO.class);
            accountDTOS.add(accountDTO);
        }
        pageInfo.setList(accountDTOS);
        return pageInfo;
    }

    @Override
    @Transactional
    public void updateAdmin(UpdateAdminParam param) {
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id",param.getAccountId());
        Account account = accountMapper.selectOne(queryWrapper);
        if (account==null) {
            throw new AccountException(AccountExceptionCode.ACCOUNT_EXCEPTION_ACCOUNT_NOT_EXIST);
        }
        UpdateWrapper<Account> wrapper = new UpdateWrapper<>();
        wrapper.set("account_nickname",param.getAccountNickname());
        wrapper.eq("account_id",param.getAccountId());
        accountMapper.update(null,wrapper);
    }

    @Override
    public void enableAccount(Integer accountId) {
        //账号是否存在
        Account account = accountMapper.selectById(accountId);
        if (account==null) {
            throw new AccountException(AccountExceptionCode.ACCOUNT_EXCEPTION_ACCOUNT_NOT_EXIST);
        }
        //查看是否是正常状态
        if (account.getAccountState()== StateData.ADMIN_STATE_NORMAL) {
            throw new AccountException(AccountExceptionCode.ACCOUNT_EXCEPTION_ADMIN_HAS_NORMAL);
        }
        //是否被删除
        if (account.getAccountState()==StateData.ADMIN_STATE_DELETED) {
            throw new AccountException(AccountExceptionCode.ACCOUNT_EXCEPTION_ADMIN_HAS_DELETED);
        }
        account.setAccountState(StateData.ADMIN_STATE_NORMAL);
        accountMapper.updateById(account);
    }

    @Override
    public void disableAccount(Integer accountId) {
        //账号是否存在
        Account account = accountMapper.selectById(accountId);
        if (account==null) {
            throw new AccountException(AccountExceptionCode.ACCOUNT_EXCEPTION_ACCOUNT_NOT_EXIST);
        }
        //查看是否已禁用
        if (account.getAccountState()== StateData.ADMIN_STATE_DISABLED) {
            throw new AccountException(AccountExceptionCode.ACCOUNT_EXCEPTION_ADMIN_HAS_DISABLED);
        }
        //是否被删除
        if (account.getAccountState()==StateData.ADMIN_STATE_DELETED) {
            throw new AccountException(AccountExceptionCode.ACCOUNT_EXCEPTION_ADMIN_HAS_DELETED);
        }
        //先禁用当前账号
        account.setAccountState(StateData.ADMIN_STATE_DISABLED);
        accountMapper.updateById(account);
    }

    @Override
    public void deleteAccount(Integer accountId) {
        //账号是否存在
        Account account = accountMapper.selectById(accountId);
        if (account==null) {
            throw new AccountException(AccountExceptionCode.ACCOUNT_EXCEPTION_ACCOUNT_NOT_EXIST);
        }
        //是否被删除
        if (account.getAccountState()==StateData.ADMIN_STATE_DELETED) {
            throw new AccountException(AccountExceptionCode.ACCOUNT_EXCEPTION_ADMIN_HAS_DELETED);
        }
        account.setAccountState(StateData.ADMIN_STATE_DELETED);
        accountMapper.updateById(account);
    }

    @Override
    public void undeleteAccount(Integer accountId) {
        //账号是否存在
        Account account = accountMapper.selectById(accountId);
        if (account==null) {
            throw new AccountException(AccountExceptionCode.ACCOUNT_EXCEPTION_ACCOUNT_NOT_EXIST);
        }
        //是否被删除
        if (account.getAccountState()!=StateData.ADMIN_STATE_DELETED) {
            throw new AccountException(AccountExceptionCode.ACCOUNT_EXCEPTION_ADMIN_NOT_DELETED);
        }
        account.setAccountState(StateData.ADMIN_STATE_NORMAL);
        accountMapper.updateById(account);
    }
}
